<!DOCTYPE html> 
<html lang="en-US">
  <head>
    <title>Holy Cross Online Result System</title>
    <meta charset="utf-8">
    <link href="<?php echo base_url(); ?>assets/css/admin/global.css" rel="stylesheet" type="text/css">
    <script src="<?php echo base_url(); ?>assets/js/jquery-2.0.3.min.js"></script>
    
    <script src="<?php echo base_url(); ?>assets/js/bootstrap.min.js"></script>
    
    <script src="<?php echo base_url(); ?>assets/js/encryption/extended5.js"></script>
    <script src="<?php echo base_url(); ?>assets/js/encryption/fouriermultiply1.js"></script>
    <script src="<?php echo base_url(); ?>assets/js/encryption/rsa2.js"></script>
  
    
  </head>
  <body>
	  <div id="mybox" ><br/><br/><br/><br/><br/></div>
    <div class="container login">
     
    <form action="<?php echo base_url(); ?>index.php?/admin/login/validate_credentials" method="post" accept-charset="utf-8" class="form-signin" id="form1" autocomplete="off">
    
      <?php 
      $attributes = array('class' => 'form-signin','id'=>'form1','autocomplete'=>'off');
     // echo form_open('admin/login/validate_credentials', $attributes);
      
      echo '<h2 class="form-signin-heading">Login</h2>';
      echo form_input('user_name', '', 'placeholder="Username" id="user_name" style="width: 210px;"');
      echo form_password('password', '', 'placeholder="Password" id="password"  style="width: 210px;" onkeyup="isEnterKey(event.keyCode,this);" ');
      if(isset($message_error) && $message_error){
          echo '<div class="alert alert-error">';
            echo '<a class="close" data-dismiss="alert">×</a>';
            echo '<strong>Oh snap!</strong> Change a few things up and try submitting again.';
          echo '</div>';             
      }
      echo "<br />";
      echo "<br />";
      echo "<br />";
      //echo form_submit('submit', 'Login', 'class="btn btn-large btn-primary" id="loadRSA"');
     //echo form_close();
      ?>
      <input type="hidden" name="h_password" id="h_password" value="">
      <input type="button" value='Login' class="btn btn-large btn-primary" id="loadRSA" >
     </form>
    </div><!--container-->
  <script type="text/javascript">  
  $("#loadRSA").click(function(event){  
      event.preventDefault();
      loadKey();
     // $('#form1').submit();
   });
  ////////////////////
  function isEnterKey(KEYCODE,OBJECT)
  {
   if(KEYCODE==13)
   {
     loadKey();
   }
  }
////////////////
  function loadKey()
   {
		var request=$.ajax({
	        type: 'POST',
	        url: '<?php echo base_url(); ?>index.php?/cycret/getKeys',
	        //data: $(this).serialize(),
	        data: "password=",
	        dataType: 'html'
	       });

		request.done(function(data) {
			   if(data!="")
			   {
		 	     ReceivedData= data.split("/"); 
		 	     key=ReceivedData[0];
		 	     mod=ReceivedData[1];
		 	     message=document.getElementById('password').value;
			     encode=rsa_encrypt(message,key,mod);
			     document.getElementById('password').value="";
			     document.getElementById('h_password').value=encode;
			     $('#form1').submit();
			     //document.getElementById("form1").submit();
			   }
		});
		
		request.fail(function(jqXHR, textStatus) {
		    alert( "Request failed: " + textStatus );
		});
		
   }//End loadKey function

 ////////////////////////////////////////////////
  function  loadKey2()
      {
	  var xhr;
	    if (window.XMLHttpRequest) {
	        xhr = new XMLHttpRequest();
	    }
	    else if (window.ActiveXObject) {
	        xhr = new ActiveXObject("Msxml2.XMLHTTP");
	    }
	    else {
	        throw new Error("Ajax is not supported by this browser");
	    }
	    xhr.onreadystatechange = function () {
	        if (xhr.readyState === 4) {
	            if (xhr.status == 200 && xhr.status < 300) {
	                alert(xhr.responseText);
	            }
	        }
	    }
	    //var url= '<?php echo base_url(); ?>index.php?/cycret/getKeys';
	    var url= "http://www.techno-bd.com/onlineresult/index.php?/cycret/getKeys";
	    xhr.open('POST', url);
	    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    xhr.send();

	  }
//////////RSA Encryption Start////////////////

  function rsa_encrypt(m,  e,  n) { 
	    var asci = new Array; 
	    for (i=0; i<m.length; i+=3) { 
	        var tmpasci="1"; 
	        for (h=0; h<3; h++) { 
	            if (i+h <m.length) { 
		            st=m.substring(i+h,  i+h+1);
	                tmpstr = st.charCodeAt (0) - 30; 

	                if (tmpstr.length < 2) { 
	                   tmpstr ="0"+tmpstr; 
	                } 
	            } else { 
	                break; 
	            } 
	            tmpasci +=tmpstr; 
	        } 
	        asci.push(tmpasci+"1"); 
	    } 

	    //Encrypt individual numbers 
	    coded = "";
	    for (k=0; k< asci.length; k++) { 
	        resultmod = powmod(asci[k],  e,  n); 
	        coded += resultmod+" "; 
	    } 
		//encrypt function returns m = x^d (mod n) 
	    return coded.trim(); 
	} 

	//Exponentiation function
	function powmod(base,  exp,  modulus) { 
	    accum = 1; 
	    i = 0; 
	    basepow2 = base; 
	    while ((exp >> i)>0) { 
	        if (((exp >> i) & 1) == 1) { 
	            accum = mo((accum*basepow2),modulus); 
	        } 
	        basepow2 = mo((basepow2 * basepow2) ,  modulus); 
	        i++; 
	    } 
	    return accum; 
	}
  //////////////////////////
	function mo(g, l) { 
		return g - (l * Math.floor(g/l)); 
	} 
	////////////////////////////

  </script>
  </body>
</html>    
    